package com.ebt.m.account.ui;

import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.ebt.m.AppContext;
import com.ebt.m.R;
import com.ebt.m.account.AccountServiceManager;
import com.ebt.m.account.AccountConstant;
import com.ebt.m.activity.LoginActivity;
import com.ebt.m.commons.utils.ToastUtil;
import com.ebt.m.utils.InputCheckUtil;
import com.ebt.m.utils.StringUtils;
import com.ebt.m.utils.TalkingDataUtil;
import com.ebt.m.utils.UIHelper;
import com.ebt.m.utils.os.HandlerBase;
import com.ebt.m.widget.BaseActivity;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;


/**
 * 创建时间：2014-11-12 上午9:53:31 项目名称：EBT.assist
 *
 * @author allen.li
 * @version 1.0
 * @since JDK 1.6.0_21 文件名称：ActLogin.java 类说明：
 */
public class ActResetPwd extends BaseActivity implements OnClickListener {

    @BindView(R.id.return_button)
    ImageView bt_return;
    @BindView(R.id.center_textview)
    TextView tv_title;
    @BindView(R.id.et_confirmNewPwd)
    EditText et_confirmNewPwd;
    @BindView(R.id.et_newPwd)
    EditText et_newPwd;
    @BindView(R.id.btn_next)
    Button btn_next;
    private String userName;
    private String phone;

    private ProgressDialog progressBar;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_reset_pwd);
        ButterKnife.bind(this);
        initView();
        initData();
    }

    public void initView() {
        tv_title.setText(getStr(R.string.reset_pwd));
        btn_next.setOnClickListener(this);
        btn_next.setText(getStr(R.string.confirm));
    }

    public void initData() {
        Bundle it = getIntent().getExtras();
        if (it == null) {
            toast("未获取到绑定的宜保通账号。");
            finish();
        } else {
            userName = it.getString(AccountConstant.USER_NAME);
            phone = it.getString(AccountConstant.USER_PHONE);
            if (StringUtils.isEmpty(userName)) {
                toast("未获取到绑定的宜保通账号。");
                finish();
            } else {
            }
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_next:
                TalkingDataUtil.onEvent(TalkingDataUtil.TYPE_RESET_PWD);
                if (checkInput()) {
                    final Handler handlerResetPassword = new HandlerBase(AppContext.getInstance()) {
                        @Override
                        public void handleMessage(Message msg) {
                            if (progressBar != null) {
                                progressBar.cancel();
                            }
                            super.handleMessage(msg);
                            switch (msg.what) {
                                case AccountServiceManager.MSG_ACCOUNT_OK:
                                    ToastUtil.show(ActResetPwd.this, "密码重置成功");
                                    Intent intent = new Intent(ActResetPwd.this, LoginActivity.class);
                                    intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
                                    intent.setAction(LoginActivity.ACTION_FROM_RESETPWD);
                                    Bundle bundle = new Bundle();
                                    bundle.putString(LoginActivity.USERNAME_KEY, phone);
                                    bundle.putString(LoginActivity.PASSWORD_KEY, getStr(et_newPwd));
                                    intent.putExtras(bundle);
                                    startActivity(intent);

                                    break;
                                case AccountServiceManager.PWD_CHANGE_ERROR:
                                    ToastUtil.show(ActResetPwd.this, "密码重置失败");
                                    break;
                                default:
                                    break;
                            }
                        }
                    };

                    if (progressBar != null) {
                        progressBar.cancel();
                        progressBar = null;
                    }
                    progressBar = new ProgressDialog(mContext);
                    progressBar.setTitle("请稍等");
                    progressBar.setMessage("正在重置密码...");
                    progressBar.setCancelable(false);
                    progressBar.setButton(DialogInterface.BUTTON_NEGATIVE, "取消", new DialogInterface.OnClickListener() {

                        @Override
                        public void onClick(DialogInterface arg0, int arg1) {
                            if (progressBar != null) {
                                progressBar.dismiss();
                            }
                        }

                    });
                    progressBar.show();
                    new Thread() {
                        public void run() {
                            new AccountServiceManager().resetPassword(userName, getStr(et_newPwd), handlerResetPassword);
                        }
                    }.start();
                }
                break;
            case R.id.btn_cancel:
                finish();
                break;
            default:
                break;
        }
    }

    private boolean checkInput() {
        UIHelper.hideSoftInputFromWindow(this);
        String confirmNewPwd = getStr(et_confirmNewPwd);
        String newPwd = getStr(et_newPwd);

        if (StringUtils.isEmpty(newPwd)) {
            toast(R.string.message_empty_new_password);
            return false;
        }

        if (StringUtils.isEmpty(confirmNewPwd)) {
            toast(R.string.message_empty_confirm_password);
            return false;
        }

        if (!newPwd.equals(confirmNewPwd)) {
            toast(R.string.message_password_different);
            return false;
        }

        if (!InputCheckUtil.passwordCheck(newPwd)) {
            toast(R.string.message_password_constraint);
            return false;
        }

        if (newPwd.length() < 8 || newPwd.length() > 12) {
            toast(R.string.message_password_length);
            return false;
        }

        return true;
    }

    @OnClick(R.id.return_button)
    void cancle(View v) {
        finish();
    }


}
